package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    // dubbo消费者
    @Reference
    UserService userService;

    // 从SpringSecurity中获取用户信息
    @RequestMapping(value = "/findUsername")
    public Result findUsername(){
        try {
            // 只要是认证通过，使用SecurityContextHolder来存放用户信息
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            // 成功
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            // 失败
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    //分页查询用户信息
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return  userService.findPage(queryPageBean);
    }


    //添加用户
    @RequestMapping("add")//注意：@RequestParam("roleIds")可以省略不写，因为参数名都为：roleIds
    @PreAuthorize(value = "hasAuthority('USER_ADD')")   //添加权限
    public Result add(@RequestBody com.itheima.health.pojo.User user, @RequestParam("roleIds")Integer[] roleIds){
        try {
            userService.add(user, roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }

    //根据id查询用户，用于表单数据回显
    @RequestMapping("findById")
    public com.itheima.health.pojo.User findByUserId(Integer id) {
        return  userService.findById(id);
    }

    //根据用户id查询该用户所拥有的所有角色id
    @RequestMapping("findRoleIdsByUserId")
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return userService.findRoleIdsByUserId(id);
    }

    //编辑用户
    @RequestMapping("editUser")
    @PreAuthorize(value = "hasAuthority('USER_EDIT')")
    public Result editUser(@RequestBody com.itheima.health.pojo.User user, Integer[] roleIds) {
        try {
            userService.editUser(user, roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }


    //删除用户信息
    @RequestMapping("deleteUser")
    @PreAuthorize(value = "hasAuthority('USER_DELETE')")
    public Result deleteUser(Integer id) {
        try {
            userService.deleteUser(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    //查询所有用户信息
    @RequestMapping("findAll")
    @PreAuthorize(value = "hasAuthority('USER_QUERY')")
    public List<com.itheima.health.pojo.User> findAll(){
        return userService.findAll();
    }
}
